package com.example.anew.journey.mynewjourney.logic.setPassword.persenter;

import android.content.Context;
import android.text.TextUtils;

import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.logic.setPassword.bean.UserRegistrationInformationBean;
import com.example.anew.journey.mynewjourney.logic.setPassword.model.ISSetPasswordModel;
import com.example.anew.journey.mynewjourney.logic.setPassword.model.SetPasswordModel;
import com.example.anew.journey.mynewjourney.logic.setPassword.view.ISSetPasswordView;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.Map;

import cn.jesse.nativelogger.NLogger;
import okhttp3.Call;
import okhttp3.Response;


/**
 * 杭州惟君医疗科技有限公司10
 * Created by 20433 on 2018/3/7
 */

public class SetPasswordPersenter {
    private ISSetPasswordView mISSetPasswordView;
    private Map<String, String> map;
    private ISSetPasswordModel mSetPasswordModel;

    public SetPasswordPersenter(ISSetPasswordView iSSetPasswordView) {
        this.mISSetPasswordView = iSSetPasswordView;
        mSetPasswordModel = new SetPasswordModel();
    }

    /**
     * @desc 注册
     */
    public void userRegistration( Context mContext) {
        map = mISSetPasswordView.carryRegistrationInformation();
       String state =  mISSetPasswordView.getThirdPartyLoginSTATE();
       NLogger.e("TAG","=========state============"+state);
        if(state == null){
            String phone = map.get("phone");
            if (TextUtils.isEmpty(phone)) {
                mISSetPasswordView.showToastInformation("请输入手机号");
                return;
            }
            String version_code = map.get("version_code");
            if (TextUtils.isEmpty(phone)) {
                mISSetPasswordView.showToastInformation("请输入验证码");
                return;
            }
            String niname = map.get("niname");
            if (TextUtils.isEmpty(phone)) {
                mISSetPasswordView.showToastInformation("请输入昵称");
                return;
            }
            String password = map.get("password");
            if (TextUtils.isEmpty(phone)) {
                mISSetPasswordView.showToastInformation("请输入密码");
                return;
            }
            mISSetPasswordView.displayProgress();
            submitUserRegistrationInformation(phone, version_code, niname, password,mContext);
        }else{
            if(state.equals("1")){
                String phone = map.get("phone").replace(" ","");

                if (TextUtils.isEmpty(phone)) {
                    mISSetPasswordView.showToastInformation("请输入手机号");
                    return;
                }

                String niname = map.get("niname");
                if (TextUtils.isEmpty(niname)) {
                    mISSetPasswordView.showToastInformation("请输入昵称");
                    return;
                }
                String password = map.get("password");
                if (TextUtils.isEmpty(password)) {
                    mISSetPasswordView.showToastInformation("请输入密码");
                    return;
                }
                String version_code = map.get("version_code");
                if (TextUtils.isEmpty(phone)) {
                    mISSetPasswordView.showToastInformation("请输入验证码");
                    return;
                }
                String name =  mISSetPasswordView.getThirdPartyLoginNAME();
                String ID =  mISSetPasswordView.getThirdPartyLoginID();
                NLogger.e("TAG","=========state====name========"+name);
                NLogger.e("TAG","=========state====ID========"+ID);
                NLogger.e("TAG","=========state=====123243535345======="+phone);
                NLogger.e("TAG","=========state====niname========"+niname);
                NLogger.e("TAG","=========state=====password======="+password);
                NLogger.e("TAG","=========state=====version_code======="+version_code);
                ThirdPartyRegistered(name,ID,phone,niname,password,mContext,version_code);
            }

        }

    }

    private void ThirdPartyRegistered(final String name, final String user_id, String phone, String niname, String password, final Context mContext,String version_code) {
        String url = RBConstants.SERVER_URL+RBConstants.THIRD_PARTY_REGISTERED;
        NLogger.e("TAG","=========LoginWecht===56=====url===="+url);
        OkHttpUtils
                .post()
                .url(url)
                .addParams("tel", phone)
                .addParams("password", password)
                .addParams("cus_name", niname)
                .addParams("code",version_code)
                .addParams(name, user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, final int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                        NLogger.e("TAG","=========LoginWecht===56========="+NetworkResponseDate);
                        final UserRegistrationInformationBean userRegistrationInformationBean =  gson.fromJson(NetworkResponseDate, UserRegistrationInformationBean.class);
                        // TODO 移植
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(userRegistrationInformationBean.errorCode == 200){
                                    mISSetPasswordView.showToastInformation("注册成功");
                                    UserRegistrationInformationBean.DataBean mbean =    userRegistrationInformationBean.data;
                                    if(mbean != null){
                                        // todo 这里要友盟账号统计
                                        mSetPasswordModel.StorageRegistrationData(mbean,mContext);
                                        if(name.equals("weixin")){
                                            mSetPasswordModel.storageThirdPartyRegisteredWecht(name,user_id,mContext);
                                        }else if(name.equals("qq")){
                                            mSetPasswordModel.storageThirdPartyRegisteredQQ(name,user_id,mContext);
                                        }
                                    }
                                    mISSetPasswordView.toMainActivity();
                                }else{
                                    mISSetPasswordView.showToastInformation(userRegistrationInformationBean.errorMessage);
                                }
                                mISSetPasswordView.hiddenProgress();
                            }
                        });

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        NLogger.e("TAG","=========LoginWecht===56e========="+e);
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 提交注册信息
     */
    private void submitUserRegistrationInformation(String phone, String version_code, String niname, String password, final Context mContext) {
        String url = RBConstants.SERVER_URL+RBConstants.REGISTERED;
        NLogger.e("TAG","=========submitUserRegistrationInformation===url========="+url);
        OkHttpUtils
                .post()
                .url(url)
                .addParams("tel", phone)
                .addParams("code",version_code)
                .addParams("password", password)
                .addParams("cus_name", niname)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson =   new Gson();
                        NLogger.e("TAG","=========submitUserRegistrationInformation===56324========="+NetworkResponseDate);
                        final UserRegistrationInformationBean userRegistrationInformationBean =  gson.fromJson(NetworkResponseDate, UserRegistrationInformationBean.class);
                        // TODO 移植
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(userRegistrationInformationBean.errorCode == 200){
                                    UserRegistrationInformationBean.DataBean mbean =    userRegistrationInformationBean.data;
                                    if(mbean != null){
                                        // todo 这里要友盟账号统计
                                        mSetPasswordModel.StorageRegistrationData(mbean,mContext);

                                    }
                                    mISSetPasswordView.toMainActivity();
                                }else{
                                    mISSetPasswordView.showToastInformation(userRegistrationInformationBean.errorMessage);
                                }
                                mISSetPasswordView.hiddenProgress();
                            }
                        });

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {


                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
